//数据库连接池
const mysql = require('mysql');

class MysqlPool {
  constructor(){
    this.flag = true;
    this.pool = mysql.createPool({
      host:"localhost",
      user:"root",
      password:"123456",
      port:"3306",
      database:"mock",//库名
    });
    this.poolQuery=(sql, values)=>{
      return new Promise((resolve, reject)=>{
        this.pool.query(sql, values,(err ,result)=>{
          if(err){
            reject(err)
          }else{
            resolve(result)
          }
        })
      })
    }
  }
  getPool(){
    if(this.flag){
      this.pool.on('connection', (connection)=>{
        connection.query('SET SESSION auto_increment_increment=1');
        this.flag = false;
      });
    }
    // return this.pool;
    return this.poolQuery
  }  
}


module.exports = MysqlPool;